cmake_minimum_required(VERSION 3.2)

find_package(Java REQUIRED)
include(UseJava)

project(Common)

find_package(Protobuf REQUIRED)

add_jar(FrancaIDL MANIFEST src/MANIFEST.MF
	src/fidl/Cli.java
	src/fidl/GeneratorStruct.java
	src/fidl/GeneratorProto.java
	INCLUDE_JARS lib/*
)
add_custom_command(TARGET FrancaIDL POST_BUILD
	COMMAND mkdir -p ${CMAKE_SOURCE_DIR}/bin
	COMMAND cp FrancaIDL.jar ${CMAKE_SOURCE_DIR}/bin
	VERBATIM)

set(IFACE_DIR ${CMAKE_SOURCE_DIR}/../Core/iface)
list(APPEND FrancaIDL_files
	${IFACE_DIR}/com/fidl/types.fidl
	${IFACE_DIR}/com/fidl/iface.fidl
)
add_custom_command(OUTPUT Generate_headers
	COMMAND java -Dfile.encoding=utf8 -jar FrancaIDL.jar
	ARGS -s -d include -n header ${FrancaIDL_files}
	VERBATIM)

add_custom_command(OUTPUT Generate_protos
	COMMAND java -Dfile.encoding=utf8 -jar FrancaIDL.jar
	ARGS -p -d proto -n proto ${FrancaIDL_files}
		-o TreatEnumAsInt=true -o TreatTypedefAsMessage=false
	VERBATIM)
add_custom_target(interface ALL
	DEPENDS FrancaIDL Generate_headers Generate_protos)
add_custom_command(TARGET interface POST_BUILD
	COMMAND mkdir -p ${CMAKE_SOURCE_DIR}/proto
	COMMAND cp -r include ${CMAKE_SOURCE_DIR}
	COMMAND cp -r proto ${CMAKE_SOURCE_DIR}
	VERBATIM)

PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS
	proto/proto/com/fidl/types.proto
	proto/proto/com/fidl/iface.proto
)
include_directories(
	${CMAKE_CURRENT_BINARY_DIR}
	${CMAKE_SOURCE_DIR}/include
	/home/woody/local/include
)
link_directories(/home/woody/local/lib)

add_library(test_target SHARED
	${PROTO_SRCS}
)
add_dependencies(test_target interface)
target_link_libraries(test_target ${Protobuf_LIBRARY})

